package com.hopedove.workshop.ui

import android.app.DatePickerDialog
import android.os.Bundle
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.LiveData
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hopedove.base.BaseActivity
import com.hopedove.data.Datetime
import com.hopedove.workshop.R
import com.hopedove.workshop.data.pojo.ExchangeBatchItem
import com.hopedove.workshop.databinding.WorkshopExchangeBatchHomeActivityBinding
import com.hopedove.workshop.ui.adapter.ExchangeBatchConfirmAdapter
import com.hopedove.workshop.viewmodel.WorkshopExchangeBatchViewModel
import org.koin.androidx.viewmodel.ext.android.viewModel

/**
 *  换批确认界面
 *
 */
class ExchangeBatchConfirmActivity : BaseActivity() {

    private lateinit var binding: WorkshopExchangeBatchHomeActivityBinding
    private val viewModel: WorkshopExchangeBatchViewModel by viewModel()

    private lateinit var datePickerDialog: DatePickerDialog

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding =
            DataBindingUtil.setContentView(this, R.layout.workshop_exchange_batch_home_activity)
        with(binding) {
            lifecycleOwner = this@ExchangeBatchConfirmActivity
            vm = viewModel

            rvList.layoutManager =
                LinearLayoutManager(this@ExchangeBatchConfirmActivity, RecyclerView.VERTICAL, false)
        }

        setupViewModelObservers()
        viewModel.load()
    }

    private fun setupViewModelObservers() {
        observePickDate(viewModel.pickDate)
        observeToastText(viewModel.toastText)
        observeLoading(viewModel.loading)

        observeExchangeBatchList(viewModel.exchangeBatchList)
    }

    private fun observePickDate(event: LiveData<Datetime>) {
        event.observe(this, { date ->
            if (!::datePickerDialog.isInitialized) {
                datePickerDialog =
                    DatePickerDialog(
                        this,
                        datePickerListener,
                        date.year(),
                        date.month() - 1,
                        date.dayOfMonth()
                    )
            }

            if (datePickerDialog.isShowing) datePickerDialog.dismiss()
            datePickerDialog.updateDate(date.year(), date.month() - 1, date.dayOfMonth())
            datePickerDialog.show()
        })
    }

    private fun observeExchangeBatchList(data: LiveData<List<ExchangeBatchItem>>) {
        data.observe(this, { list ->
            with(binding.rvList) {

                val batchAdapter = adapter as? ExchangeBatchConfirmAdapter
                batchAdapter?.updateDataSet(list)
                if (batchAdapter == null) {
                    adapter = ExchangeBatchConfirmAdapter().apply {
                        updateDataSet(list)
                    }
                }
            }
        })
    }

    private val datePickerListener =
        DatePickerDialog.OnDateSetListener { _, year, month, dayOfMonth ->
            viewModel.updateDate(Datetime(year, month + 1, dayOfMonth))

            // refresh list?

        }

}